﻿using AutoMapper;
using ERPSystem.Common.Domains.Material;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace BasicInformationCommon.API.BasicInformationCommons.Materiales
{
    public class MaterialesBcakFillHander:IRequestHandler<MaterialesBcakFillCommand,Result<MaterialclassifyDTO>>

    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Measureunit> measureunitRepository;
        private readonly IBaseRepository<Supplymanner> supplymannerRepository;
        private readonly IBaseRepository<Classifyzt> classifyztRepository;
        private readonly IBaseRepository<MaterialCascade> materialCascadeRepository;

        public MaterialesBcakFillHander(IMapper mapper, IBaseRepository<Measureunit> measureunitRepository, IBaseRepository<Supplymanner> supplymannerRepository, IBaseRepository<Classifyzt> classifyztRepository, IBaseRepository<MaterialCascade> materialCascadeRepository)
        {
            this._mapper = mapper;
            this.measureunitRepository = measureunitRepository;
            this.supplymannerRepository = supplymannerRepository;
            this.classifyztRepository = classifyztRepository;
            this.materialCascadeRepository = materialCascadeRepository;
        }

        /// <summary>
        /// 物料分类反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<MaterialclassifyDTO>> Handle(MaterialesBcakFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<MaterialclassifyDTO>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var measureunit = await (await measureunitRepository.GetAll()).ToListAsync();//计量单位
                var supplymanner = await (await supplymannerRepository.GetAll()).ToListAsync();//供应方式
                var classifyzt = await (await classifyztRepository.GetAll()).ToListAsync();//分类状态
                var materialCascade = await (await materialCascadeRepository.GetAll()).ToListAsync();//物料分类（大表）

                var cc = (from a in materialCascade
                          join b in measureunit on a.MeasureId equals b.MeasureId
                          join c in supplymanner on a.SupplyId equals c.SupplyId
                          join d in classifyzt on a.ClassifystateId equals d.ClassifystateId
                          where a.MaterialTypeId == request.MaterialTypeId
                          select new MaterialclassifyDTO()
                          {
                              MaterialTypeId = a.MaterialTypeId,
                              MaterialTypeName = a.MaterialTypeName,
                              Serialnumber = a.Serialnumber,
                              Mclassifyencode = a.Mclassifyencode,
                              MeasureId = b.MeasureId,
                              MeasureName = b.MeasureName,
                              SupplyId = c.SupplyId,
                              SupplyName = c.SupplyName,
                              ClassifystateId = d.ClassifystateId,
                              ClassifystateName = d.ClassifystateName

                          }).FirstOrDefault();



                if (cc != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = cc;
                }

        
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
